package com.yaorange.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yaorange.dao.DeptMapper;
import com.yaorange.entity.Dept;
import com.yaorange.service.DeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Set;


@Service
@Transactional
public class DeptServiceImpl implements DeptService {

    @Resource
    private DeptMapper deptMapper;


    //模糊查询+分页
    @Override
    public PageInfo<Dept> getPageByCondition(Integer pageNum, Integer pageSize, String conditionDeptName) {

        PageHelper.startPage(pageNum,pageSize);
        //设置查询条件
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        //搜索用户名
        if(conditionDeptName != null && !"".equals(conditionDeptName)){
            criteria.andLike("deptName","%"+conditionDeptName+"%");
        }
        //1代表删除了的信息
        criteria.andNotEqualTo("deptState",1);
        List<Dept> depts = deptMapper.selectByExample(example);
        //组装返回PageInfo
        return new PageInfo<>(depts);
    }


    //增
    @Override
    public int addDept(Dept dept) {
        int insert = deptMapper.insert(dept);
        return insert;
    }

    //删、改会用
    @Override
    public Dept getDept(Integer deptId) {
        Dept dept = deptMapper.selectByPrimaryKey(deptId);
        return dept;
    }

    //删、改会用
    @Override
    public int updateDept(Dept dept) {
        int i = deptMapper.updateByPrimaryKeySelective(dept);
        return i;
    }

    //找到所有的部门
    @Override
    public List<Dept> getDeptList() {
        List<Dept> depts = deptMapper.selectAll();
        return depts;
    }

    @Override
    public List<Dept> getDeptAndEmpList(Set<Integer> deptIds) {
        return deptMapper.selectListByIds(deptIds);
    }

    @Override
    public List<Dept> getBusinessDeptList() {
        Example example = new Example(Dept.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andNotEqualTo("deptState",1);//不获取已删除的部门
        criteria.andLike("deptName","%业务%");//获取关键字为“业务”的部门
        return deptMapper.selectByExample(example);
    }


}
